﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Historia.Formulaires;

namespace Historia.Controls
{
    /// <summary>
    /// Interaction logic for ctrElements.xaml
    /// </summary>
    public partial class ctrElements : UserControl
    {
        #region Membres
        protected DataManager _dm = null;
        #endregion

        #region Constructeur
        
        
        public ctrElements()
        {
            InitializeComponent();
        }

        #endregion

        #region Méthodes

        public void Init(ref DataManager dm)
        {
            this._dm = dm;
            this.DisplayData();
        }

        protected void DisplayData()
        {
            this.ChargerTreeViewPersonnages();
            this.ChargerTreeViewLieux();
            this.ChargerTreeViewItems();
            this.ChargerTreeViewFils();
            
        }

        protected void ChargerTreeViewPersonnages() {
            TreeViewItem tviCat = null;
            string categorie = null;

            // On reset le treeview
            this.tvPersonnages.Items.Clear();

            // Affiche les personnages
            foreach (DataRow p in this._dm.DB.Tables[DataManager.PERSONNAGES].Select(null, "categorie ASC, nom ASC"))
            {

                // Si le personnage fait partie d'une catégorie, le personnage
                // sera dans le répertoire de cette categorie
                if (!string.IsNullOrEmpty(p.Field<string>("categorie")))
                {

                    // Est-ce une nouvelle categorie?
                    if ((tviCat == null) || (!p.Field<string>("categorie").ToString().Equals(categorie)))
                    {
                        tviCat = new TreeViewItem();

                        Image img = new Image();
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.UriSource = new Uri(@"pack://application:,,,/Images/folder-icon.png", UriKind.RelativeOrAbsolute);
                        bi.DecodePixelWidth = 15;
                        bi.EndInit();
                        img.Source = bi;
                        img.Width = 15;
                        img.Height = 15;
                        
                        TextBlock txt = new TextBlock();
                        txt.Text = p.Field<string>("categorie");
                        txt.Padding = new Thickness(2);
                        txt.Margin = new Thickness(3, 0, 0, 0);
                        txt.FontWeight = FontWeights.Bold;

                        StackPanel stack = new StackPanel();
                        stack.Orientation = Orientation.Horizontal;
                        stack.Children.Add(img);
                        stack.Children.Add(txt);

                        tviCat.Header = stack;
                        tviCat.IsExpanded = true;
                        categorie = p.Field<string>("categorie");
                    }

                }

                TreeViewItem tviPerso = this.CreerTreeviewItemPersonnage(p);

                if (tviCat != null)
                {
                    tviCat.Items.Add(tviPerso);
                    if (!this.tvPersonnages.Items.Contains(tviCat))
                    {
                        this.tvPersonnages.Items.Add(tviCat);
                    }
    
                }
                else
                {
                    this.tvPersonnages.Items.Add(tviPerso);
                }
            }
        }

        protected TreeViewItem CreerTreeviewItemPersonnage(DataRow personnage) {
            
            TreeViewItem tviPerso = new TreeViewItem();

            Image img = new Image();
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri(@"pack://application:,,,/Images/person-icon.png",UriKind.RelativeOrAbsolute);
            bi.DecodePixelWidth = 15;
            bi.EndInit();
            img.Source = bi;
            img.Width = 15;
            img.Height = 15;

            TextBlock txt = new TextBlock();
            txt.Text = personnage.Field<string>("nom") + " (" + personnage.Field<string>("abreviation") + ")";
            txt.Padding = new Thickness(2);
            txt.Margin = new Thickness(3, 0, 0, 0);

            StackPanel stack = new StackPanel();
            stack.Orientation = Orientation.Horizontal;
            stack.Children.Add(img);
            stack.Children.Add(txt);

            tviPerso.Header = stack;
            tviPerso.Tag = personnage;

            return tviPerso;
        }

        protected void ChargerTreeViewLieux()
        {
            TreeViewItem tviCat = null;
            string categorie = null;

            // On reset le treeview
            this.tvLieux.Items.Clear();

            // Affiche les lieux
            foreach (DataRow dr in this._dm.DB.Tables[DataManager.LIEUX].Select(null, "categorie ASC, nom ASC"))
            {

                // Si le lieu fait partie d'une catégorie, le lieu
                // sera dans le répertoire de cette categorie
                if (!string.IsNullOrEmpty(dr.Field<string>("categorie")))
                {

                    // Est-ce une nouvelle categorie?
                    if ((tviCat == null) || (!dr.Field<string>("categorie").ToString().Equals(categorie)))
                    {
                        tviCat = new TreeViewItem();

                        Image img = new Image();
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.UriSource = new Uri(@"pack://application:,,,/Images/folder-icon.png", UriKind.RelativeOrAbsolute);
                        bi.DecodePixelWidth = 15;
                        bi.EndInit();
                        img.Source = bi;
                        img.Width = 15;
                        img.Height = 15;

                        TextBlock txt = new TextBlock();
                        txt.Text = dr.Field<string>("categorie");
                        txt.Padding = new Thickness(2);
                        txt.Margin = new Thickness(3, 0, 0, 0);
                        txt.FontWeight = FontWeights.Bold;

                        StackPanel stack = new StackPanel();
                        stack.Orientation = Orientation.Horizontal;
                        stack.Children.Add(img);
                        stack.Children.Add(txt);

                        tviCat.Header = stack;
                        tviCat.IsExpanded = true;
                        categorie = dr.Field<string>("categorie");
                    }

                }

                TreeViewItem tviLieu = this.CreerTreeviewItemLieu(dr);

                if (tviCat != null)
                {
                    tviCat.Items.Add(tviLieu);
                    if (!this.tvLieux.Items.Contains(tviCat))
                    {
                        this.tvLieux.Items.Add(tviCat);
                    }

                }
                else
                {
                    this.tvLieux.Items.Add(tviLieu);
                }
            }
        }

        protected TreeViewItem CreerTreeviewItemLieu(DataRow lieu)
        {

            TreeViewItem tviLieu = new TreeViewItem();

            Image img = new Image();
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri(@"pack://application:,,,/Images/pin-icon.png", UriKind.RelativeOrAbsolute);
            bi.DecodePixelWidth = 15;
            bi.EndInit();
            img.Source = bi;
            img.Width = 15;
            img.Height = 15;

            TextBlock txt = new TextBlock();
            txt.Text = lieu.Field<string>("nom");
            txt.Padding = new Thickness(2);
            txt.Margin = new Thickness(3, 0, 0, 0);

            StackPanel stack = new StackPanel();
            stack.Orientation = Orientation.Horizontal;
            stack.Children.Add(img);
            stack.Children.Add(txt);

            tviLieu.Header = stack;
            tviLieu.Tag = lieu;

            return tviLieu;
        }

        protected void ChargerTreeViewItems()
        {
            TreeViewItem tviCat = null;
            string categorie = null;

            // On reset le treeview
            this.tvItems.Items.Clear();

            // Affiche les items
            foreach (DataRow dr in this._dm.DB.Tables[DataManager.ITEMS].Select(null, "categorie ASC, nom ASC"))
            {

                // Si l'item fait partie d'une catégorie, l'item
                // sera dans le répertoire de cette categorie
                if (!string.IsNullOrEmpty(dr.Field<string>("categorie")))
                {

                    // Est-ce une nouvelle categorie?
                    if ((tviCat == null) || (!dr.Field<string>("categorie").ToString().Equals(categorie)))
                    {
                        tviCat = new TreeViewItem();

                        Image img = new Image();
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.UriSource = new Uri(@"pack://application:,,,/Images/folder-icon.png", UriKind.RelativeOrAbsolute);
                        bi.DecodePixelWidth = 15;
                        bi.EndInit();
                        img.Source = bi;
                        img.Width = 15;
                        img.Height = 15;

                        TextBlock txt = new TextBlock();
                        txt.Text = dr.Field<string>("categorie");
                        txt.Padding = new Thickness(2);
                        txt.Margin = new Thickness(3, 0, 0, 0);
                        txt.FontWeight = FontWeights.Bold;

                        StackPanel stack = new StackPanel();
                        stack.Orientation = Orientation.Horizontal;
                        stack.Children.Add(img);
                        stack.Children.Add(txt);

                        tviCat.Header = stack;
                        tviCat.IsExpanded = true;
                        categorie = dr.Field<string>("categorie");
                    }

                }

                TreeViewItem tviItem = this.CreerTreeviewItemItem(dr);

                if (tviCat != null)
                {
                    tviCat.Items.Add(tviItem);
                    if (!this.tvItems.Items.Contains(tviCat))
                    {
                        this.tvItems.Items.Add(tviCat);
                    }

                }
                else
                {
                    this.tvItems.Items.Add(tviItem);
                }
            }
        }

        protected TreeViewItem CreerTreeviewItemItem(DataRow Item)
        {

            TreeViewItem tviItem = new TreeViewItem();

            Image img = new Image();
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri(@"pack://application:,,,/Images/fusil-icon.png", UriKind.RelativeOrAbsolute);
            bi.DecodePixelWidth = 15;
            bi.EndInit();
            img.Source = bi;
            img.Width = 15;
            img.Height = 15;

            TextBlock txt = new TextBlock();
            txt.Text = Item.Field<string>("nom");
            txt.Padding = new Thickness(2);
            txt.Margin = new Thickness(3, 0, 0, 0);

            StackPanel stack = new StackPanel();
            stack.Orientation = Orientation.Horizontal;
            stack.Children.Add(img);
            stack.Children.Add(txt);

            tviItem.Header = stack;
            tviItem.Tag = Item;

            return tviItem;
        }

        protected void ChargerTreeViewFils()
        {
            // On reset le treeview
            this.tvFils.Items.Clear();

            // Affiche les fils
            foreach (DataRow dr in this._dm.DB.Tables[DataManager.FILS].Select(null, "nom ASC"))
            {
                TreeViewItem tviFil = this.CreerTreeviewItemFil(dr);
                this.tvFils.Items.Add(tviFil);
             
            }
        }

        protected TreeViewItem CreerTreeviewItemFil(DataRow Item)
        {

            TreeViewItem tviFil = new TreeViewItem();

            Image img = new Image();
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri(@"pack://application:,,,/Images/fusil-icon.png", UriKind.RelativeOrAbsolute);
            bi.DecodePixelWidth = 15;
            bi.EndInit();
            img.Source = bi;
            img.Width = 15;
            img.Height = 15;

            TextBlock txt = new TextBlock();
            txt.Text = Item.Field<string>("nom") + " (" + Item.Field<string>("abreviation") + ")";
            txt.Padding = new Thickness(2);
            txt.Margin = new Thickness(3, 0, 0, 0);

            StackPanel stack = new StackPanel();
            stack.Orientation = Orientation.Horizontal;
            stack.Children.Add(img);
            stack.Children.Add(txt);

            tviFil.Header = stack;
            tviFil.Tag = Item;

            return tviFil;
        }

        protected void NouvelleElement(){
                switch (this.tabElements.SelectedIndex)
            {
                case 0:
                    frmPersonnage frmP = new frmPersonnage();

                    if (frmP.ShowFormDialog(ref this._dm, null) == true)
                    {
                        this.ChargerTreeViewPersonnages();
                    }
                    break;
                case 1:
                    frmLieu frmL = new frmLieu();

                    if (frmL.ShowFormDialog(ref this._dm, null) == true)
                    {
                        this.ChargerTreeViewLieux();
                    }
                    break;
                case 2:
                    frmItem frmI = new frmItem();

                    if (frmI.ShowFormDialog(ref this._dm, null) == true)
                    {
                        this.ChargerTreeViewItems();
                    }
                    break;
                case 3:
                    frmFil frmF = new frmFil();

                    if (frmF.ShowFormDialog(ref this._dm, null) == true)
                    {
                        this.ChargerTreeViewFils();
                    }
                    break;
                default:
                    break;
            }
        }

        protected void ModifierElement()
        {
              
                switch (this.tabElements.SelectedIndex)
                {
                    case 0:
                        if (this.tvPersonnages.SelectedItem != null)
                        {

                            if (((TreeViewItem)this.tvPersonnages.SelectedItem).Tag != null)
                            {
                                frmPersonnage frmP = new frmPersonnage();

                                if (frmP.ShowFormDialog(ref this._dm, ((DataRow)((TreeViewItem)this.tvPersonnages.SelectedItem).Tag).Field<Int32>("id_personnage")) == true)
                                {
                                    this.ChargerTreeViewPersonnages();
                                }
                            }
                        }
                        break;
                    case 1:
                        if (this.tvLieux.SelectedItem != null)
                        {

                            if (((TreeViewItem)this.tvLieux.SelectedItem).Tag != null)
                            {
                                frmLieu frmL = new frmLieu();

                                if (frmL.ShowFormDialog(ref this._dm, ((DataRow)((TreeViewItem)this.tvLieux.SelectedItem).Tag).Field<Int32>("id_lieu")) == true)
                                {
                                    this.ChargerTreeViewLieux();
                                }
                            }
                        }
                        break;
                    case 2:
                        if (this.tvItems.SelectedItem != null)
                        {

                            if (((TreeViewItem)this.tvItems.SelectedItem).Tag != null)
                            {
                                frmItem frmI = new frmItem();

                                if (frmI.ShowFormDialog(ref this._dm, ((DataRow)((TreeViewItem)this.tvItems.SelectedItem).Tag).Field<Int32>("id_item")) == true)
                                {
                                    this.ChargerTreeViewItems();
                                }
                            }
                        }
                        break;
                    case 3:
                        if (this.tvFils.SelectedItem != null)
                        {

                            if (((TreeViewItem)this.tvFils.SelectedItem).Tag != null)
                            {
                                frmFil frmF = new frmFil();

                                if (frmF.ShowFormDialog(ref this._dm, ((DataRow)((TreeViewItem)this.tvFils.SelectedItem).Tag).Field<Int32>("id_fil")) == true)
                                {
                                    this.ChargerTreeViewFils();
                                }
                            }
                        }
                        break;
                    default:
                        break;
                }
        }

        protected void SupprimerElement()
        {

            switch (this.tabElements.SelectedIndex)
            {
                case 0:
                    if (this.tvPersonnages.SelectedItem != null)
                    {

                        if (((TreeViewItem)this.tvPersonnages.SelectedItem).Tag != null)
                        {
                            if (MessageBox.Show("Êtes-vous certains de vouloir supprimer cet élément?", "Supprimer", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                            {
                                DataRow dr = (DataRow)((TreeViewItem)this.tvPersonnages.SelectedItem).Tag;
                                dr.Delete();
                                if (((TreeViewItem)this.tvPersonnages.SelectedItem).Parent is TreeViewItem)
                                {
                                    TreeViewItem parent = (TreeViewItem)((TreeViewItem)this.tvPersonnages.SelectedItem).Parent;
                                    parent.Items.Remove(this.tvPersonnages.SelectedItem);

                                    if (parent.Items.Count == 0)
                                    {
                                        this.tvPersonnages.Items.Remove(parent);
                                    }
                                }
                                else
                                {
                                    this.tvPersonnages.Items.Remove((TreeViewItem)this.tvPersonnages.SelectedItem);
                                }

                            }
                        }
                    }
                    break;
                case 1:
                    if (this.tvLieux.SelectedItem != null)
                    {

                        if (((TreeViewItem)this.tvLieux.SelectedItem).Tag != null)
                        {
                            if (MessageBox.Show("Êtes-vous certains de vouloir supprimer cet élément?", "Supprimer", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                            {
                                DataRow dr = (DataRow)((TreeViewItem)this.tvLieux.SelectedItem).Tag;
                                dr.Delete();
                                if (((TreeViewItem)this.tvLieux.SelectedItem).Parent is TreeViewItem)
                                {
                                    TreeViewItem parent = (TreeViewItem)((TreeViewItem)this.tvLieux.SelectedItem).Parent;
                                    parent.Items.Remove(this.tvLieux.SelectedItem);

                                    if (parent.Items.Count == 0)
                                    {
                                        this.tvLieux.Items.Remove(parent);
                                    }
                                }
                                else
                                {
                                    this.tvLieux.Items.Remove((TreeViewItem)this.tvLieux.SelectedItem);
                                }
                                
                            }
                        }
                    }
                    break;
                case 2:
                    if (this.tvItems.SelectedItem != null)
                    {

                        if (((TreeViewItem)this.tvItems.SelectedItem).Tag != null)
                        {
                            if (MessageBox.Show("Êtes-vous certains de vouloir supprimer cet élément?", "Supprimer", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                            {
                                DataRow dr = (DataRow)((TreeViewItem)this.tvItems.SelectedItem).Tag;
                                dr.Delete();
                                if (((TreeViewItem)this.tvItems.SelectedItem).Parent is TreeViewItem)
                                {
                                    TreeViewItem parent = (TreeViewItem)((TreeViewItem)this.tvItems.SelectedItem).Parent;
                                    parent.Items.Remove(this.tvItems.SelectedItem);

                                    if (parent.Items.Count == 0)
                                    {
                                        this.tvItems.Items.Remove(parent);
                                    }
                                }
                                else
                                {
                                    this.tvItems.Items.Remove((TreeViewItem)this.tvItems.SelectedItem);
                                }

                            }
                        }
                    }
                    break;
                case 3:
                    if (this.tvFils.SelectedItem != null)
                    {

                        if (((TreeViewItem)this.tvFils.SelectedItem).Tag != null)
                        {
                            if (MessageBox.Show("Êtes-vous certains de vouloir supprimer cet élément?", "Supprimer", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                            {
                                DataRow dr = (DataRow)((TreeViewItem)this.tvFils.SelectedItem).Tag;
                                dr.Delete();
                                this.tvFils.Items.Remove((TreeViewItem)this.tvFils.SelectedItem);
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        }

        protected void AssignerElement()
        {
            switch (this.tabElements.SelectedIndex)
            {
                case 0:
                    if (!(this.tvPersonnages.SelectedItem == null))
                    {
                        AssignElementRoutedEventArgs e = new AssignElementRoutedEventArgs(AssignElementClickEvent, this);
                        e.ElementType = AssignElementRoutedEventArgs.AssignElementTypeEnum.Personnage;
                        e.Element = (TreeViewItem)this.tvPersonnages.SelectedItem;
                        this.RaiseEvent(e);
                    }
                    break;
                case 1:
                    if (!(this.tvLieux.SelectedItem == null))
                    {
                        AssignElementRoutedEventArgs e = new AssignElementRoutedEventArgs(AssignElementClickEvent, this);
                        e.ElementType = AssignElementRoutedEventArgs.AssignElementTypeEnum.Lieu;
                        e.Element = (TreeViewItem)this.tvLieux.SelectedItem;
                        this.RaiseEvent(e);
                    }
                    break;
                case 2:
                    if (!(this.tvItems.SelectedItem == null))
                    {
                        AssignElementRoutedEventArgs e = new AssignElementRoutedEventArgs(AssignElementClickEvent, this);
                        e.ElementType = AssignElementRoutedEventArgs.AssignElementTypeEnum.Item;
                        e.Element = (TreeViewItem)this.tvItems.SelectedItem;
                        this.RaiseEvent(e);
                    }
                    break;
                case 3:
                    if (!(this.tvFils.SelectedItem == null))
                    {
                        AssignElementRoutedEventArgs e = new AssignElementRoutedEventArgs(AssignElementClickEvent, this);
                        e.ElementType = AssignElementRoutedEventArgs.AssignElementTypeEnum.Fil;
                        e.Element = (TreeViewItem)this.tvFils.SelectedItem;
                        this.RaiseEvent(e);
                    }
                    break;
                default:
                    break;
            }
        }

        protected void AgrandirTout() {
            switch (this.tabElements.SelectedIndex)
            {
                case 0:
                   foreach (TreeViewItem t in this.tvPersonnages.Items){
                       t.IsExpanded = true;
                   }
                    break;
                case 1:
                    foreach (TreeViewItem t in this.tvLieux.Items)
                    {
                        t.IsExpanded = true;
                    }
                    break;
                case 2:
                    foreach (TreeViewItem t in this.tvItems.Items)
                    {
                        t.IsExpanded = true;
                    }
                    break;
                case 3:
                    // On fait rien
                    break;
                default:
                    break;
            }
        }

        protected void RefermerTout()
        {
            switch (this.tabElements.SelectedIndex)
            {
                case 0:
                    foreach (TreeViewItem t in this.tvPersonnages.Items)
                    {
                        t.IsExpanded = false;
                    }
                    break;
                case 1:
                    foreach (TreeViewItem t in this.tvLieux.Items)
                    {
                        t.IsExpanded = false;
                    }
                    break;
                case 2:
                    foreach (TreeViewItem t in this.tvItems.Items)
                    {
                        t.IsExpanded = false;
                    }
                    break;
                case 3:
                    // On fait rien
                    break;
                default:
                    break;
            }
        }

        protected void MAJInformation() {
            TableRow row = new TableRow();
            TableCell cel = new TableCell();
            Paragraph para = new Paragraph();

            this.tblInfo.RowGroups[0].Rows.Clear();

            switch (this.tabElements.SelectedIndex)
            {
                case 0:
                    if (this.tvPersonnages.SelectedItem != null)
                    {

                        if (((TreeViewItem)this.tvPersonnages.SelectedItem).Tag != null)
                        {
                            DataRow dr = (DataRow)((TreeViewItem)this.tvPersonnages.SelectedItem).Tag;
                            
                            //Nom
                            cel = new TableCell();
                            row = new TableRow();
                            para.Inlines.Add(new Run("Nom:"));
                            para.FontWeight = System.Windows.FontWeights.Bold;
                            cel.Blocks.Add(para);
                            row.Cells.Add(cel);
                            cel = new TableCell(new Paragraph(new Run(dr.Field<string>("nom") + " (" + dr.Field<string>("abreviation") + ")")));
                            row.Cells.Add(cel);
                            this.tblInfo.RowGroups[0].Rows.Add(row);

                           //Age
                            if (dr.Field<Int16?>("age").HasValue)
                            {
                                cel = new TableCell();
                                row = new TableRow();
                                para = new Paragraph(new Run("Âge:"));
                                para.FontWeight = System.Windows.FontWeights.Bold;
                                cel.Blocks.Add(para);
                                row.Cells.Add(cel);
                                cel = new TableCell(new Paragraph(new Run(dr.Field<Int16>("age").ToString())));
                                row.Cells.Add(cel);
                                this.tblInfo.RowGroups[0].Rows.Add(row);
                            }

                            //Sexe
                            cel = new TableCell();
                            row = new TableRow();
                            para = new Paragraph(new Run("Sexe:"));
                            para.FontWeight = System.Windows.FontWeights.Bold;
                            cel.Blocks.Add(para);
                            row.Cells.Add(cel);
                            cel = new TableCell(new Paragraph(new Run(dr.Field<string>("sexe"))));
                            row.Cells.Add(cel);
                            this.tblInfo.RowGroups[0].Rows.Add(row);

                            //Role
                            cel = new TableCell();
                            row = new TableRow();
                            para = new Paragraph(new Run("Rôle:"));
                            para.FontWeight = System.Windows.FontWeights.Bold;
                            cel.Blocks.Add(para);
                            row.Cells.Add(cel);
                            cel = new TableCell(new Paragraph(new Run(dr.Field<string>("role"))));
                            row.Cells.Add(cel);
                            this.tblInfo.RowGroups[0].Rows.Add(row);

                            //Description
                            cel = new TableCell();
                            row = new TableRow();
                            para = new Paragraph(new Run("Description:"));
                            para.FontWeight = System.Windows.FontWeights.Bold;
                            cel.Blocks.Add(para);
                            row.Cells.Add(cel);
                            cel = new TableCell(new Paragraph(new Run(dr.Field<string>("Description"))));
                            row.Cells.Add(cel);
                            this.tblInfo.RowGroups[0].Rows.Add(row);
                             
                        }
                    }
                    break;
                case 1:
                    if (this.tvLieux.SelectedItem != null)
                    {

                        if (((TreeViewItem)this.tvLieux.SelectedItem).Tag != null)
                        {
                            DataRow dr = (DataRow)((TreeViewItem)this.tvLieux.SelectedItem).Tag;
                            //Nom
                            cel = new TableCell();
                            row = new TableRow();
                            para = new Paragraph(new Run("Nom:"));
                            para.FontWeight = System.Windows.FontWeights.Bold;
                            cel.Blocks.Add(para);
                            row.Cells.Add(cel);
                            cel = new TableCell(new Paragraph(new Run(dr.Field<string>("nom"))));
                            row.Cells.Add(cel);
                            this.tblInfo.RowGroups[0].Rows.Add(row);

                            //Description
                            cel = new TableCell();
                            row = new TableRow();
                            para = new Paragraph(new Run("Description:"));
                            para.FontWeight = System.Windows.FontWeights.Bold;
                            cel.Blocks.Add(para);
                            row.Cells.Add(cel);
                            cel = new TableCell(new Paragraph(new Run(dr.Field<string>("Description"))));
                            row.Cells.Add(cel);
                            this.tblInfo.RowGroups[0].Rows.Add(row);

                        }
                    }
                    break;
                case 2:
                    if (this.tvItems.SelectedItem != null)
                    {

                        if (((TreeViewItem)this.tvItems.SelectedItem).Tag != null)
                        {
                            DataRow dr = (DataRow)((TreeViewItem)this.tvItems.SelectedItem).Tag;
                            //Nom
                            cel = new TableCell();
                            row = new TableRow();
                            para = new Paragraph(new Run("Nom:"));
                            para.FontWeight = System.Windows.FontWeights.Bold;
                            cel.Blocks.Add(para);
                            row.Cells.Add(cel);
                            cel = new TableCell(new Paragraph(new Run(dr.Field<string>("nom"))));
                            row.Cells.Add(cel);
                            this.tblInfo.RowGroups[0].Rows.Add(row);

                            //Description
                            cel = new TableCell();
                            row = new TableRow();
                            para = new Paragraph(new Run("Description:"));
                            para.FontWeight = System.Windows.FontWeights.Bold;
                            cel.Blocks.Add(para);
                            row.Cells.Add(cel);
                            cel = new TableCell(new Paragraph(new Run(dr.Field<string>("Description"))));
                            row.Cells.Add(cel);
                            this.tblInfo.RowGroups[0].Rows.Add(row);
                        }
                    }
                    break;
                case 3:
                    if (this.tvFils.SelectedItem != null)
                    {

                        if (((TreeViewItem)this.tvFils.SelectedItem).Tag != null)
                        {
                            DataRow dr = (DataRow)((TreeViewItem)this.tvFils.SelectedItem).Tag;
                            //Nom
                            cel = new TableCell();
                            row = new TableRow();
                            para = new Paragraph(new Run("Nom:"));
                            para.FontWeight = System.Windows.FontWeights.Bold;
                            cel.Blocks.Add(para);
                            row.Cells.Add(cel);
                            cel = new TableCell(new Paragraph(new Run(dr.Field<string>("nom") + " (" + dr.Field<string>("abreviation") + ")")));
                            row.Cells.Add(cel);
                            this.tblInfo.RowGroups[0].Rows.Add(row);
                        }
                    }
                    break;
                default:
                    break;
            }

        }

        #endregion

        #region Events

        public static readonly RoutedEvent AssignElementClickEvent = EventManager.RegisterRoutedEvent(
    "AssignElementClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ctrChapitres));

        // Provide CLR accessors for the event
        public event RoutedEventHandler AssignElementClick
        {
            add { AddHandler(AssignElementClickEvent, value); }
            remove { RemoveHandler(AssignElementClickEvent, value); }
        }

        #endregion

        private void btnNouveau_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.NouvelleElement();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void btnModifier_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.ModifierElement();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void btnSupprimer_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.SupprimerElement();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void btnAgrandirTout_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.AgrandirTout();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void btnRefermerTout_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.RefermerTout();
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void tvPersonnages_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            try
            {
                this.MAJInformation();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void tvLieux_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            try
            {
                this.MAJInformation();
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void tvItems_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            try
            {
                this.MAJInformation();
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void tvFils_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            try
            {
                this.MAJInformation();
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void tabElements_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                this.MAJInformation();
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btnAssigner_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.AssignerElement();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
